package org.vin.tempctl.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.vin.tempctl.service.TemperatureCTL;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

@RestController
public class TemperatureController {
    @Autowired
   private TemperatureCTL ctl ;

    @ResponseBody
    @GetMapping("getTem")
    //@RequestMapping(value = "/hello", method = RequestMethod.GET)
    public ResponseEntity<Object> getTemperature() throws Exception {
        System.out.println("--getTemperature--");
        int temperature = ctl.getTemperature();
        return new ResponseEntity<>(temperature,HttpStatus.OK);
    }

    @ResponseBody
    @GetMapping("getTemSetting")
    //@RequestMapping(value = "/hello", method = RequestMethod.GET)
    public ResponseEntity<Object> getTemperatureSetting() throws Exception {
        System.out.println("--getTemperature--");
        int temperature = ctl.getTemperatureSetting();
        return new ResponseEntity<>(temperature,HttpStatus.OK);
    }

    @ResponseBody
    @RequestMapping("/setTem/{tem}")
    public ResponseEntity<Object> setTemperature(@PathVariable int tem) throws Exception {
        long currentTimeMillis = System.currentTimeMillis();
        Date date = new Date(currentTimeMillis);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis), ZoneId.systemDefault());
        System.out.println("--setTemperature--" + localDateTime+"--"+tem);
        boolean flag = ctl.setTemperature(tem);
        return new ResponseEntity<>(flag,HttpStatus.OK);
    }

    @ResponseBody
    @RequestMapping("/setTemRate/{temRate}")
    public ResponseEntity<Object> setTemRate(@PathVariable int temRate) throws Exception {
        System.out.println("--setTemRate--");

        boolean flag = ctl.setTemRate(temRate);
        return new ResponseEntity<>(flag,HttpStatus.OK);
    }

    @ResponseBody
    @RequestMapping("/setOn")
    public ResponseEntity<Object> setOn() throws Exception {
        System.out.println("--setOn--");

        boolean flag = ctl.setON();
        return new ResponseEntity<>(flag,HttpStatus.OK);
    }

    @ResponseBody
    @RequestMapping("/setOff")
    public ResponseEntity<Object> setOFF() throws Exception {
        System.out.println("--setOFF--");

        boolean flag = ctl.setOFF();
        return new ResponseEntity<>(flag,HttpStatus.OK);
    }


}